package cn.edu.ujn.lab3.web;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.deser.Deserializers.Base;

import cn.edu.ujn.lab3.dao.BaseDict;
import cn.edu.ujn.lab3.service.BaseDictService;
import cn.edu.ujn.lab3.service.CustomerService;
import cn.edu.ujn.lab3.utils.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
public class BaseDictController {
	
	@Autowired
	private BaseDictService baseDictService;
	

	@RequestMapping(value = "/basedict/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows, 
			String dictItemName, String dictTypeName,  String dictTypeCode,Model model) {
		List<BaseDict> selectAll = baseDictService.selectAll();
		//所有数据字典类型
		List<BaseDict> dictTypeList = new ArrayList<BaseDict>();
		// 遍历数据库中的所有数据字典列表
		for (BaseDict basedict : selectAll) {
			Boolean exist = false;
			for (BaseDict baseDcitType : dictTypeList) {
				// 已存在则结束此次循环
				if (baseDcitType.getDict_type_code().equals(basedict.getDict_type_code())) {
					exist = true;
					break;
				}
			}
			//循环完不存在则添加
			if (exist == false) {
				dictTypeList.add(basedict);
			}
		}
		// 查询数据
		Page<BaseDict> baseDicts = this.baseDictService.findBaseDictList(page, rows, dictItemName, dictTypeName,dictTypeCode);
		// 是否启用
		HashMap<String, String> isEable = new HashMap<String, String>();
		isEable.put("1", "是");
		isEable.put("0", "否");
		// 添加参数
		model.addAttribute("dictTypeList", dictTypeList);		//下拉列表
		model.addAttribute("page", baseDicts);					// 所有数据
		model.addAttribute("isEnable", isEable);				// 是否启动的下拉列表
		return "basedict";
	}
	

	
	@RequestMapping("/basedict/create.action")
	@ResponseBody
	public String baseDictCreate(String dictTypeCode, String dictTypeName,String dictItemName, String dictEnable) {
	    BaseDict baseDict = new BaseDict();

		baseDict.setDict_item_name(dictItemName);
		baseDict.setDict_type_name(dictTypeName);
		baseDict.setDict_type_code(dictTypeCode);
		baseDict.setDict_enable(dictEnable);
		
		// 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = this.baseDictService.insertSelective(baseDict);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	

	@RequestMapping("/basedict/getBaseDictById.action")
	@ResponseBody
	public BaseDict getBaseDictById(Integer id) {
		BaseDict baseDict = this.baseDictService.selectByPrimaryKey(id);
	    return baseDict;
	}
	

	@RequestMapping("/basedict/update.action")
	@ResponseBody
	public String baseDictUpdate(BaseDict updateBaseDict) {
		int rows = this.baseDictService.updateByPrimaryKey(updateBaseDict);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
}



